package com.topvision.myviewtest.camera;

import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.widget.FrameLayout;

public class CameraView extends FrameLayout {
    private LayoutParams mMatchParrent;
    private Context mAppContext;
    private CameraPreview mCameraPreview;

    public CameraView(Context context) {
        this(context, null);
    }

    public CameraView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CameraView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        mMatchParrent = new LayoutParams(-1, -1);
        mAppContext = context;
        reloadViews();
    }

    private void reloadViews() {
        removeAllViews();
        mCameraPreview = new CameraPreview(mAppContext);
        addView(mCameraPreview, mMatchParrent);
    }

    /** 外部调用函数 */


    /** 打开关闭Camera */
    public void enableCamera(Boolean enable) {
        if (enable) {
            mCameraPreview.startCamera();
        } else {
            mCameraPreview.stopCamera();
        }
    }

    public void pauseCamera() {
        mCameraPreview.pauseCamera();
    }

    public void resumeCamera() {
        mCameraPreview.resumeCamera();
    }

    public Camera getCamera() {
        return mCameraPreview.getCamera();
    }

    /** 设置焦距 */
    public void setZoom(int value, boolean isAdd) {
        mCameraPreview.setZoom(value, isAdd);
    }

    /** 设置帧数据回调 */
    public void setPreviewCallback(OnPreviewCallback previewCallback) {
        mCameraPreview.setPreviewCallback(previewCallback);
    }
}